package com.lizhangjun.nim.controller;


import com.lizhangjun.nim.constant.IGroup;
import com.lizhangjun.nim.entity.Result;
import com.lizhangjun.nim.session.SessionManager;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;

/**
 * IM管理接口
 * @author lizhangjun
 * @since 2020-08-27
 */
@Slf4j
@RestController
@RequestMapping("/v1/api")
public class ApiController {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 创建用户组
     * @param group 用户组信息
     * @return 用户组
     */
    @PostMapping("/create/group")
    @PreAuthorize("hasRole('ROLE_AR')")
    public Result createGroup(@RequestBody IGroup group){
        group.setAddTime(System.currentTimeMillis());
        IGroup save = mongoTemplate.save(group);
        return Result.ok(save);
    }

    /**
     * 获取连接数
     * @return 连接数
     */
    @PermitAll
    @GetMapping("/count")
    public Result count(){
        String count = SessionManager.count();
        return Result.ok(count);
    }
}
